/**
 * 详情对话框
 */
var ShelfInfoDlg = {
    data: {
        id: "",
        shelvesNum: "",
        shelvesName: "",
        warehouseId: "",
        warehouseName: "",
        productId: "",
        productName: "",
        region: "",
        row: "",
        floor: "",
        mark: "",
        status: "",
        remark: "",
        handleUser: "",
        createTime: "",
        createUser: "",
        auditUser: "",
        auditTime: "",
        updateTime: "",
        printTime: "",
        updateUser: "",
        isDelete: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/shelf/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('shelfForm', result.data);

    $.ajax({
        url: '/baseCommonSelect/initWarehouseInfos',
        dataType: 'json',
        // data: {"enumCode": "WarehouseType"},
        type: 'post',
        success: function(data) {
            var option;
            if (data!== null) {
                // $("#warehouseNameSelect").empty();
                // $("#warehouseNameSelect").append(new Option());
                var warehouseId = $("#warehouseId").val();
                $.each(data, function(index, item) {

                    // $('#warehouseNameSelect').append(new Option(item.value,item.id));
                    if(!warehouseId){
                         option = new Option(item.value,item.id);
                    }else {
                         option = new Option(item.value,item.id);
                        // // 如果是之前的parentId则设置选中
                        if(item.id == warehouseId) {
                            option.setAttribute("selected",'true');
                        }
                    }
                    $('#warehouseNameSelect').append(option);//往下拉菜单里添加元素
                    form.render("select");
                });
            } else {

                $("#warehouseNameSelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
        }
    });



    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/shelf/editItem", function (data) {


            Feng.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        //获取选中的值
        var warehouseId = $("#warehouseNameSelect").val();
        //获取选中文本
        var warehouseName = $("#warehouseNameSelect").find("option:selected").text();

        data.field.warehouseId = warehouseId;
        data.field.warehouseName = warehouseName;

        ajax.set(data.field);
        ajax.start();

        return false;
    });

});